package cn.edu.ujn.finallab.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ujn.finallab.dao.Orders;
import cn.edu.ujn.finallab.dao.Room;
import cn.edu.ujn.finallab.service.IRoomService;
import cn.edu.ujn.finallab.util.Page;

@Controller
public class RoomController {
	@Autowired
	private IRoomService roomService;

	@RequestMapping("/bookRoom")
	public String bookRoom(Model model) {
		model.addAttribute("msg", "请输入查询信息");
		return "bookRoom";
	}

	@RequestMapping("/roomSearch")
	public String roomSearch(HttpServletRequest request, Model model) {
		// 获取参数
		String roomType = request.getParameter("roomType");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");

		// 将数据价格转换成int型返回到前端
		String low = request.getParameter("lowPrice");
		String high = request.getParameter("highPrice");
		Integer lowPrice = null;
		Integer highPrice = null;
		if (!low.equals("") && !high.equals("")) {
			Double lowDPrice = Double.parseDouble(low);
			Double highDPrice = Double.parseDouble(high);
			lowPrice = lowDPrice.intValue();
			highPrice = highDPrice.intValue();
		}
		// 将传过来的参数插入Map里，传到数据库
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("pType", roomType);
		map.put("pLowPrice", lowPrice);
		map.put("pHighPrice", highPrice);

		List<Room> result = new ArrayList<Room>();
		result.addAll(roomService.findUnreservedRoomByTypeAndPriceRoom(map));
		
		List<Room> reservedRoomLists = roomService.findReservedRoomByTypeAndPrice(map);
		// 遍历找到的每个房间
		for (Room r : reservedRoomLists) {
			// 得到该房间的所有订单，判断是否符合输入时间段的条件
			List<Orders> ordersList = r.getOrderLists();
			// 将订单表按入住时间从小到大排序
			Collections.sort(ordersList, new Comparator<Orders>() {
				@Override
				public int compare(Orders o1, Orders o2) {
					return o1.getInTime().compareTo(o2.getInTime());
				}
			});

			int n = ordersList.size();
			for (int i = 0; i < n; i++) {// 如果输入的开始时间大于上一个订单的结束时间，输入的结束时间大于下一个订单干的开始时间，则该房间能预定
				if (i < n - 1) {
					Orders o1 = ordersList.get(i);
					Orders o2 = ordersList.get(i + 1);
					if (startTime.compareTo(o1.getOutTime()) > 0 && endTime.compareTo(o2.getInTime()) < 0) {
						result.add(r);
						break;
					}
				} else {// 如果找到了最后一个订单，则只用判断输入的开始时间是否大于最后一个订单的结束时间。大于该房则可预订
					Orders o1 = ordersList.get(i);
					if (startTime.compareTo(o1.getOutTime()) > 0) {
						result.add(r);
						break;
					}
				}

			}
		}
		
		//如果未查到任何房间，则返回错误信息
		if (result.isEmpty()) model.addAttribute("msg", "未查询到符合条件的房间");
		model.addAttribute("roomList", result);
		model.addAttribute("roomType", roomType);
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("lowPrice", lowPrice);
		model.addAttribute("highPrice", highPrice);
		return "bookRoom";
	}

	// 分页查询所有客房
	// 每页10条数据
	@RequestMapping("/allRoom")
	public String allRoom(@RequestParam(defaultValue = "1") int currentPage, Model model) {
		Page<Room> page = roomService.selectByPage(currentPage);
		model.addAttribute("pagemsg", page);
		return "roomManager";
	}

	// 删除客房
	// 删除客房时判断该客房的状态，只有在客房是空闲时可以删除
	@RequestMapping("/deleteRoom")
	public String deleteRoom(String number, int currentPage, Model model) {
		Room selectByNumber = roomService.selectByNumber(number);
		if (!"未预定".equals(selectByNumber.getState())) {
			model.addAttribute("msg", "当前该客房不空闲，无法删除！");
			Page<Room> page = roomService.selectByPage(currentPage);
			model.addAttribute("pagemsg", page);
			return "roomManager";
		} else {
			int deleteRoom = roomService.deleteRoom(selectByNumber.getId());
			return "redirect:allRoom";
		}
	}

	// 根据客房号查询当前客房信息，并显示到前端供管理员修改
	// 还需要传过来当前的currentPage
	@RequestMapping("/modifyRoom")
	public String modifyRoom(String number, int currentPage, Model model) {
		Room room = this.roomService.selectByNumber(number);
		model.addAttribute("room", room);
		model.addAttribute("currentPage", currentPage);
		return "modifyRoom";
	}

	@RequestMapping("/modifyroom")
	public String modifyroom(int currentPage, String number, String state, String type, double price, String decription, Model model) {
		Room room = roomService.selectByNumber(number);
		room.setState(room.getState());
		room.setType(type);
		room.setPrice(price);
		room.setDecription(decription);
		int updateRoom = roomService.updateRoom(room);
		Page<Room> page = roomService.selectByPage(currentPage);
		model.addAttribute("pagemsg", page);
		return "roomManager";
	}

	// 添加客房页面跳转
	@RequestMapping("/addRoom")
	public String addRoom(int currentPage, int lastPage, Model model) {
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("lastPage", lastPage);
		return "addRoom";
	}

	// 向数据库中添加客房
	@RequestMapping("/addroom")
	public String addroom(int currentPage, String number, String type, double price, String decription, Model model) {
		//首先查询是否已经存在该客房，有则返回提示
		Room room = roomService.selectByNumber(number);
		System.out.println(room);
		if(room != null) {
			model.addAttribute("msg", "该客房已存在！");
			model.addAttribute("currentPage", currentPage);
			model.addAttribute("lastPage", currentPage);
			return "addRoom";
		} else {
			Room room1 = new Room();
			room1.setNumber(number);
			room1.setType(type);
			room1.setState("未预定");
			room1.setPrice(price);
			room1.setDecription(decription);
			int insertRoom = roomService.insertRoom(room1);	
			Page<Room> page = roomService.selectByPage(currentPage);
			model.addAttribute("pagemsg", page);
			return "roomManager";
		}
		
	}

	// 管理员根据房间类型搜索房间
	// 分页查询
	@RequestMapping("/selectRoomByType")
	public String selectRoomByType(String roomtype, @RequestParam(defaultValue = "1") int currentPage, Model model) {
		Page<Room> page = new Page<Room>();
		if ("全部".equals(roomtype)) {
			page = roomService.selectByPage(currentPage);
		} else {
			page = roomService.selectByPageOfRoomTYpe(currentPage, roomtype);
		}
		model.addAttribute("pagemsg", page);
		model.addAttribute("roomtype", "");
		model.addAttribute("roomtype", roomtype);
		return "roomManager";
	}

	// 管理员根据房间号搜索房间
	// 每个房间号只有一个
	@RequestMapping("/selectRoomByNumber")
	public String selectRoomByNumber(String roomnumber, @RequestParam(defaultValue = "1") int currentPage,
			Model model) {
		Room room = roomService.selectByNumber(roomnumber);
		System.out.println(room);
		Page<Room> page = new Page<Room>();
		if (room != null) {
			ArrayList<Room> rows = new ArrayList<Room>();
			rows.add(room);
			page.setRows(rows);
			page.setPage(1);
			page.setTotalpage(1);
			page.setTotal(1);
		}
		model.addAttribute("roomtype", roomnumber);
		model.addAttribute("roomtype", "");
		model.addAttribute("pagemsg", page);
		return "roomManager";
	}
}
